package org.wak.dialog.sci
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.getTimer;

   /**
	* Dispatched when the user clicks anywhere.
	* 
	* @eventType flash.events.Event.COMPLETE
	*/
	[Event(name="COMPLETE", type="flash.events.Event")]
	
	
	/**
	 * Textbox for spoken text from characters in the SCI interface.  
	 * 
	 */
	public class SciDialogTextDisplay extends Sprite
	{
		// text field to display
		private var mField:TextField = null ;
		
		// shadow filter
		private static var mShadow:DropShadowFilter = null ;
		
		// array of assigned DialogDisplay
		private static var mDialogDisplaysArray:Array = null ;
				
		// text formater
		private var mTextFormat:TextFormat = null ;
		
		// BG Color.
		private var mBgColor:uint = 0xFFFFFF ;
		
		// background display object.
		private var mBackground:DisplayObject = null ;
		
		private var mDisplaySize:Point = null ;
		
		private var mPortrait:MovieClip = null ;
		
		private var mDisplayInfoMode:Boolean = false ;
		private var mClickCounter:int = 0 ;
		
		private var mStartTime:int = 0 ;
		private var mClickOverlapThreshold:int = 200 ;
		
		/**
		 * Constructor
		 *  
		 * @param pText Text to say or show.
		 * @param pPortrait Image of the character speaking.
		 * @param pBg Background image.
		 * @param pIsDisplay true if this is a display info only, false for conversations. 
		 * @param pFont Font type used.
		 * @param pFSize Font size used.
		 * @param pColor Font color used.
		 * @param pBold Font with bold style.
		 * @param pEmbedFont True if use embed fonts.
		 * 
		 */		
		public function SciDialogTextDisplay(pText:String, pPortrait:MovieClip, pBg:DisplayObject, pIsDisplay:Boolean, 
							pFont:String = "acknowtt", pFSize:int = 18, pColor:uint = 0xFFFFFF, pBold:Boolean = false, pEmbedFont:Boolean = true )
		{
			super();
			mDisplaySize = new Point();
			
			if(mDialogDisplaysArray == null)
				mDialogDisplaysArray = new Array();
			
			if(mShadow == null)	
				mShadow = new DropShadowFilter(5, 45, 0x000000, 0.5, 4, 4, 4, BitmapFilterQuality.LOW, false, false, false);
				
			mTextFormat = new TextFormat();
			mTextFormat.align = TextFormatAlign.LEFT ;
			mTextFormat.font = pFont ;
			mTextFormat.size = pFSize ;
			mTextFormat.color = pColor ;
			mTextFormat.bold = pBold ;
			
			mBackground = pBg ;
			if(mBackground != null)
				this.addChild(mBackground);
				
			
			mField = new TextField();
			mField.defaultTextFormat = mTextFormat;
			mField.embedFonts = pEmbedFont ;
			mField.selectable = false ;
			mField.wordWrap = true ;
			
			mField.border = false ;
			
			mField.text = pText ;
			
			this.addChild(mField);
			
			
			if( pPortrait != null )
			{
				pPortrait.x = 2 ;
				pPortrait.y = 2 ;
				
				this.addChild( pPortrait );
				mPortrait = pPortrait ;
			}
			
			mDisplayInfoMode = pIsDisplay ;
			
			this.addEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
		}
		
		protected function OnAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
			mStartTime = getTimer();
			
			//this.addEventListener(MouseEvent.CLICK,OnMouseClick);
			if(mBackground == null)
			{
				var fBg:Sprite = new Sprite();
				fBg.graphics.beginFill( mBgColor,1);
				fBg.graphics.drawRect(0,0, mDisplaySize.x, mDisplaySize.y);
				fBg.graphics.endFill();
				fBg.graphics.lineStyle( 1,0x000000);
				fBg.graphics.drawRect(0,0,mDisplaySize.x, mDisplaySize.y);
				fBg.graphics.endFill();
				
				
				mBackground = fBg ;
				this.addChild(mBackground);
				this.setChildIndex(mBackground,0);
				
			}
			
			mField.width = mDisplaySize.x ;
			mField.height = mDisplaySize.y ;
			
			if(mPortrait != null)
			{
				mField.x = mPortrait.x + mPortrait.width ;
				mField.width = mField.width - mPortrait.width ;
				
			}
			
			this.stage.addEventListener(MouseEvent.CLICK, OnMouseClick );
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, OnRemovedFromStage );
		}
		
		protected function OnRemovedFromStage(event:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE, OnRemovedFromStage );
			this.stage.removeEventListener(MouseEvent.CLICK, OnMouseClick );
		}
		
		protected function OnMouseClick(event:Event):void
		{
			//trace("SciDialogTextDisplay OnMouseClick:"+mField.text);
			
			mClickCounter++ ;
			
			if(mDisplayInfoMode )
			{
				if(mClickCounter > 1) 
					this.dispatchEvent( new Event( Event.COMPLETE ) );
			}
			else
			{
				if( (getTimer() - mStartTime) > mClickOverlapThreshold )
					this.dispatchEvent( new Event( Event.COMPLETE ) );
			}
			
		}
		
		public function set bgColor(value:uint):void
		{
			mBgColor = value ;	
		}
		
		public function get bgColor():uint
		{
			return mBgColor ;
		}
		
		public function SetSize(pW:Number, pH:Number):void
		{
			mDisplaySize.x = pW ;
			mDisplaySize.y = pH ;
		}
		
		public function set shadow(value:Boolean):void
		{
			if(value)
			{
				this.filters = [mShadow];
			}
			else
			{
				this.filters = null;
			}
		}
		
		public function set leftMargin(value:Number):void
		{
			if(mPortrait != null)
			{
				mPortrait.x = value ;
				mField.x = mPortrait.x + mPortrait.width ;
			}
			else
			{
				mField.x = value ;
			}
		}
		
		public function set topMargin(value:Number):void
		{
			if(mPortrait != null)
				mPortrait.y = value ;
			mField.y = value ;
		}
		
		
		
		
		
	}
}